/**
 *
 */
package net.oschina.j2cache.util;

import org.nustaq.serialization.FSTConfiguration;

import java.io.IOException;

/**
 * 使用 FST 实现序列化
 *
 * @author winterlau
 */
public class FSTSerializer implements Serializer {
    private final static FSTConfiguration configuration = FSTConfiguration.getDefaultConfiguration();

    @Override
    public String name() {
        return "fst";
    }

    @Override
    public byte[] serialize(Object obj) throws IOException {
        return configuration.asByteArray(obj);
        //ByteArrayOutputStream out = null;
        //FSTObjectOutput fout = null;
        //try {
        //    out = new ByteArrayOutputStream();
        //    fout = new FSTObjectOutput(out);
        //    fout.writeObject(obj);
        //    fout.flush();
        //    return out.toByteArray();
        //} catch (Exception e) {
        //    throw new CacheException(e);
        //} finally {
        //    if (fout != null)
        //        try {
        //            fout.close();
        //        } catch (IOException e) {
        //        }
        //}
    }

    @Override
    public Object deserialize(byte[] bytes) throws IOException {
        if (bytes == null || bytes.length == 0)
            return null;
        return configuration.asObject(bytes);
        //FSTObjectInput in = null;
        //try {
        //    in = new FSTObjectInput(new ByteArrayInputStream(bytes));
        //    return in.readObject();
        //} catch (ClassNotFoundException e) {
        //    throw new CacheException(e);
        //} finally {
        //    if (in != null)
        //        try {
        //            in.close();
        //        } catch (IOException e) {
        //        }
        //}
    }

}
